<!--销售主体（公司）信息配置添加与修改-->
<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增页面配置' : '编辑页面配置'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="title" label="公司名称">
        <el-input v-model="dataForm.title" placeholder="请输入公司名称"></el-input>
      </el-form-item>
      <el-form-item prop="sendPhone" label="寄件人电话">
        <el-input v-model="dataForm.sendPhone" placeholder="请输入寄件人电话"></el-input>
      </el-form-item>
      <el-form-item prop="copyright" label="版权描述">
        <el-input v-model="dataForm.copyright" placeholder="请输入版权描述"></el-input>
      </el-form-item>
      <el-form-item prop="apiDomain" label="api域名">
        <el-input v-model="dataForm.apiDomain" placeholder="请输入api域名"></el-input>
      </el-form-item>
<!--      <el-form-item prop="staticDomain" label="静态域名">-->
<!--        <el-input v-model="dataForm.staticDomain" placeholder="请输入静态资源域名"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item prop="payNotify" label="支付回调">
        <el-input v-model="dataForm.payNotify" placeholder="请输入支付回调"></el-input>
      </el-form-item>
      <el-form-item prop="refundNotify" label="退款通知">
        <el-input v-model="dataForm.refundNotify" type="textarea" autosize placeholder="请输入退款通知"></el-input>
      </el-form-item>
      <el-form-item prop="logo" label="公司logo">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.logo">
            <el-image class="deduct_img" :src="dataForm.logo" :preview-src-list="[dataForm.logo]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.logo,1)"/>
          </div>
          <com-upload v-show="!dataForm.logo" @get-url="getUrlOne"></com-upload>
        </div>
        <div style="color:#909399;"><el-icon class="el-icon-info"></el-icon>建议上传400x400的图片</div>
      </el-form-item>
      <el-form-item prop="fans" label="联系二维码">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.fans">
            <el-image class="deduct_img" :src="dataForm.fans" :preview-src-list="[dataForm.fans]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.fans,2)"/>
          </div>
          <com-upload v-show="!dataForm.fans" @get-url="getUrlTwo"></com-upload>
        </div>
        <div style="color:#909399;"><el-icon class="el-icon-info"></el-icon>建议上传400x400的图片</div>
      </el-form-item>
      <el-form-item prop="poster" label="海报背景图">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.poster">
            <el-image class="deduct_img" :src="dataForm.poster" :preview-src-list="[dataForm.poster]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.poster,3)"/>
          </div>
          <com-upload v-show="!dataForm.poster" @get-url="getUrlThree"></com-upload>
        </div>
        <div style="color:#909399;"><el-icon class="el-icon-info"></el-icon>建议上传750x1334的图片</div>
      </el-form-item>
      <el-form-item prop="coverUrl" label="链接分享图">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.coverUrl">
            <el-image class="deduct_img" :src="dataForm.coverUrl" :preview-src-list="[dataForm.coverUrl]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.coverUrl,4)"/>
          </div>
          <com-upload v-show="!dataForm.coverUrl" @get-url="getUrlFour"></com-upload>
        </div>
        <div style="color:#909399;"><el-icon class="el-icon-info"></el-icon>建议上传300x400的图片</div>
      </el-form-item>
      <el-form-item prop="personalCard" label="个人名片">
        <div class="flex_center flex_wrap">
          <div class="img_con" v-if="dataForm.personalCard">
            <el-image class="deduct_img" :src="dataForm.personalCard" :preview-src-list="[dataForm.personalCard]"/>
            <i class="el-icon-error remove_img" @click="cancelImg(dataForm.personalCard,5)"/>
          </div>
          <com-upload v-show="!dataForm.personalCard" @get-url="getUrlFive"></com-upload>
        </div>
        <div style="color:#909399;"><el-icon class="el-icon-info"></el-icon>建议上传648x800的图片</div>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        title: '',	              //公司名称
        copyright: '',            //版权描述
        apiDomain: '',            //api域名
        staticDomain: '',	        //静态资源域名
        payNotify: '',	          //支付回调
        fans: '',                 //联系二维码
        poster: '',	              //海报背景图
        logo: '',	                //公司logo
        refundNotify: '',	        //退款通知
        sendPhone:'',             //寄件人电话
        coverUrl:'',              //小程序分享链接图
        personalCard:'',          //个人名片
      }
    }
  },
  computed: {
    dataRule() {
      return {
        title: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        copyright: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        apiDomain: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        payNotify: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        fans: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        poster: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        logo: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        personalCard: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        sendPhone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'},
          {pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/shopInfo/${this.dataForm.id}`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/shopInfo', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
    // 添加单图添加
    getUrlOne(getUrl){
      this.dataForm.logo = getUrl
    },
    // 添加单图添加
    getUrlTwo(getUrl){
      this.dataForm.fans = getUrl
    },
    // 添加单图添加
    getUrlThree(getUrl){
      this.dataForm.poster = getUrl
    },
    // 添加单图添加
    getUrlFour(getUrl){
      this.dataForm.coverUrl = getUrl
    },
    // 添加单图添加
    getUrlFive(getUrl){
      this.dataForm.personalCard = getUrl
    },
    // 删除服务器图片
    cancelImg(url,index) {
      //console.log(url)
      //console.log(index)
      this.$http({
        url: `/sys/oss/deleteByUrl`,
        method: 'delete',
        data: {url: url},
      }).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        index===1?this.dataForm.logo = '':''
        index===2?this.dataForm.fans = '':''
        index===3?this.dataForm.poster = '':''
        index===4?this.dataForm.coverUrl = '':''
        index===5?this.dataForm.personalCard = '':''
        if (this.dataForm.id){
          this.$http['put']('/api/shopInfo', this.dataForm).then(({data: res}) => {
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}

.img_con {
  position: relative;
  display: flex;
}

.deduct_img {
  width: 140px;
  height: 140px;
  margin-right: 10px;
  margin-bottom: 10px;
}

.remove_img {
  position: absolute;
  right: 5px;
  top: -5px;
  color: red;
  cursor: pointer;
}
</style>
